<?php

#检测网站是否能正常访问

require_once dirname(__FILE__) . '/init.php';

#加运行锁
$fileRunLockFile = dirname(__FILE__) . '/data/runtime/monitor.tkhttp.lock';
fCheckDir($fileRunLockFile);
if (file_exists($fileRunLockFile)) {
    log::w('monitor is runing!');
    die();
}
file_put_contents($fileRunLockFile, date('Y-m-d H:i:s'));
$IS_WARNING = false;

$allInstall_Subdomains = getArrayFromFile(dirname(__FILE__) . '/tkdomain.txt');
$arrVpsData = require_once dirname(__FILE__) . '/input/vpslist.config.php';
$arrTkDate = json_decode(snoopy_fetch('http://tools.u631.com/onlinecheck/locklist_tk.time.php'), true);

$TIMESTART = date('Y-m-d H:i:s');

$cacheShotDownTime = dirname(__FILE__) . '/data/shutdown-time/';

$arrFetchHttpResult = array();
$allInstall_Maindomains_Data = array();

foreach ($allInstall_Subdomains as $itemDomain) {
    $arrDomainInfo = explode('.', $itemDomain);
    if (count($arrDomainInfo) == 3) {
        if ($arrDomainInfo[0] == 'www') {
            $allInstall_Maindomains_Data[] = $itemDomain;
        }
    }
    if (count($arrDomainInfo) == 2) {
        $allInstall_Maindomains_Data[] = 'www.' . $itemDomain;
    }
}

$allInstall_Maindomains_Data = array_unique(array_filter($allInstall_Maindomains_Data));

foreach ($allInstall_Maindomains_Data as $itemDomain) {
    #$prevResContent = file_get_contents($checkResFileSave);
    $sonTimeStart = time();
    $curl = 'http://' . $itemDomain . '/kapi.php?act=onlinecheck&pass=' . md5('tom.1234');
    $sContent = snoopy_fetch($curl);

    $lastCheckTimeCacheFile = $cacheShotDownTime . substr(md5($itemDomain), 0, 3) . '/' . $itemDomain . '.txt';
    FCheckDir($lastCheckTimeCacheFile);
    $TotalShutDownTime = 0;
    if (file_exists($lastCheckTimeCacheFile)) {
        $shutDownTime = time() - file_get_contents($lastCheckTimeCacheFile);
    } else {
        $shutDownTime = 0;
    }

    $lastCheckTimeCacheFileTotal = $cacheShotDownTime . substr(md5($itemDomain), 0, 3) . '/' . $itemDomain . '_Total.txt';
    if (!file_exists($lastCheckTimeCacheFileTotal)) {
        file_put_contents($lastCheckTimeCacheFileTotal, 0);
    } else {
        file_put_contents($lastCheckTimeCacheFileTotal, intval(file_get_contents($lastCheckTimeCacheFileTotal) + $shutDownTime));
    }

    $sonTimeEnd = time();
    if ($sContent == 'success' || $sContent == '<pre>success' || 'Operation can not be empty' == $sContent || (substr($sContent, 0, 2) == 'v9' && substr($sContent, -4) == 'news')) {
        $arrFetchHttpResult['green'][$itemDomain]['cost'] = $sonTimeEnd - $sonTimeStart;
        $arrFetchHttpResult['green'][$itemDomain]['timeerror'] = $shutDownTime;
        $arrFetchHttpResult['green'][$itemDomain]['http_code'] = $http_response_header[0];
        @unlink($lastCheckTimeCacheFile);
        $arrFetchHttpResult['green'][$itemDomain]['curl'] = $curl;
    } else {
        $arrFetchHttpResult['red'][$itemDomain]['cost'] = $sonTimeEnd - $sonTimeStart;
        $arrFetchHttpResult['red'][$itemDomain]['timeerror'] = $shutDownTime;
        $arrFetchHttpResult['red'][$itemDomain]['http_code'] = $http_response_header[0];
        if (!file_exists($lastCheckTimeCacheFile)) {
            file_put_contents($lastCheckTimeCacheFile, time());
        }
        $arrFetchHttpResult['red'][$itemDomain]['curl'] = $curl;
    }
}

$HTMLBODY = '';
$HTMLBODY.= '
    <b>TK域名HTTP遍历访问监控报告(总计:' . count($allInstall_Maindomains_Data) . '个,异常:' . count($arrFetchHttpResult['red']) . '个)</b>
	<hr />
	<b>Stat By [' . getLocalIp() . '],统计开始于:' . $TIMESTART . ',结束于' . date('Y-m-d H:i:s') . '</b>
    <table border="1" width="100%" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0" style="border-collapse: collapse;font-size: 14px;">';
$HTMLBODY.='
	<tr height="35">
    <td width="150">域名&nbsp;</td>
	<td width="100">域名过期时间</td>
	<td width="100">机器IP</td>
	<td width="100">机器有效期</td>
    <td width="150">发生时间&nbsp;</td>
    <td width="100">手工Check&nbsp;</td>
    <td width="100">域名剩余时间</td>
    <td width="100">宕机持续</td>
    <td width="100">请求耗时</td>
	</tr>
    ';

if (empty($arrFetchHttpResult['red'])) {
    $HTMLBODY.='<hr />全部正常!<hr />';
} else {
    $IS_WARNING = true;
    foreach ($arrFetchHttpResult['red'] as $itemDomain => $arrItemInfo) {
        $arrDoamin = explode('.', $itemDomain);
        $Maindomain = $arrDoamin[1] . '.' . $arrDoamin[2];

        #机器IP
        $hostip = get_hostip_by_name($maindomain);

        $HTMLBODY.='
	<tr height="35">
    <td width="150"><font' . $color . '>' . $itemDomain . '</font></td>
	<td width="100"><font' . $color . '> - </font></td>
	<td width="100">' . $hostip . '</td>
	<td width="100">' . $arrVpsData[$hostip]['exp'] . '</td>
    <td width="150">' . date('Y-m-d H:i:s') . '</td>
    <td width="100"><a href="' . $arrItemInfo['curl'] . '" target="_blank">手工检测</a></td>
    <td width="100">' . $arrTkDate[$maindomain] . '</td>
    <td width="100">' . ($arrItemInfo['timeerror'] / 3600) . '小时</td>
    <td width="100"><font' . $color . '>' . $arrItemInfo['cost'] . '秒</font>&nbsp;</td>
	</tr>
    ';
    }
}


$HTMLBODY.='</table>';
$rf = dirname(__FILE__) . '/data/http/' . date('Y-m-d') . '/' . date('H') . '_report.html';
FcheckDir($rf);
file_put_contents($rf, $HTMLBODY);

$html = $HTMLBODY;

if ($IS_WARNING) {
    post_email_by_sae('TK-HTTP访问监控统计[警告(' . count($arrFetchHttpResult['red']) . '个)][正常(' . count($arrFetchHttpResult['green']) . '个)][' . date('Y-m-d H:i:s') . ']', $html, $arrUsers);
    post_email_by_smtp('TK-HTTP访问监控统计[警告(' . count($arrFetchHttpResult['red']) . '个)][正常(' . count($arrFetchHttpResult['green']) . '个)]', $html, $arrUsers);
} else {
    post_email_by_sae('TK-HTTP访问监控统计[全部正常][正常(' . count($arrFetchHttpResult['green']) . '个)][' . date('Y-m-d H:i:s') . ']', $html, $arrUsers);
}
@unlink($fileRunLockFile);
?>